<template>
  <el-row type="flex" align="middle" justify="space-between" style="height: 40px;width: 100%">
    <el-col :span="20">{{ nodeData.name }}</el-col>
    <el-col :span="4">
      <el-row type="flex">
        <!-- 两个内容 -->
        <el-col>
          {{ nodeData.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown @command="handleCommand">
            <span @click.stop>
              操作<i class="el-icon-arrow-down" />
            </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isShow" command="edit">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isShow" command="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { reqDelDepartments } from '@/api/deparments'
export default {
  props: {
    // 接收树形控件的数据
    nodeData: {
      type: Object,
      required: true
    },
    isShow: {
      type: Boolean,
      default: false
    }
  },

  methods: {
    async handleCommand(type) {
      if (type === 'add') {
        // console.log('添加操作')
        this.$emit('add-depts', this.nodeData)
      }
      if (type === 'edit') {
        console.log('编辑操作')
        this.$emit('edit-dialog', this.nodeData)
      }
      if (type === 'del') {
        // 提示
        // res是confim的promise返回的一个结果
        const str = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(() => {
          this.$message.info('已取消删除')
        })
        if (str === 'confirm') {
          // 发送请求删除数据
          const { message } = await reqDelDepartments(this.nodeData.id)
          // 提示用户
          this.$message.success(message)
          // 子传夫通知父组件重新渲染列表
          this.$emit('del-depts')
        }
      }
    }
  }
}
</script>

<style>

</style>
